package uk.ac.strath.cis.religionsearch.view;


import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Arrays;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.jdesktop.swingx.MultiSplitLayout;
import org.jdesktop.swingx.MultiSplitPane;
import org.jdesktop.swingx.MultiSplitLayout.Divider;
import org.jdesktop.swingx.MultiSplitLayout.Leaf;
import org.jdesktop.swingx.MultiSplitLayout.Split;

import uk.ac.strath.cis.religionsearch.controller.Controller;
import uk.ac.strath.cis.religionsearch.model.Model;
import uk.ac.strath.cis.religionsearch.view.components.MenuBar;

public class SearchWindow implements Observer, ActionListener, FocusListener, ListSelectionListener, KeyListener{
	
	private JFrame mainWindow;
	private JTextField searchTextField,
					   tagsTextField;
	private JPanel container,
				   optionsPanel,
				   textsPanel,
				   versesPanel,
				   previewPanel,
				   resultsPanel,
				   chapterPanel;
	private JTextPane preview;
	private JList religiousTexts,
	              books,
				  chapters;
	private JLabel tagsLabel,
				   prvwTstmt,
				   prvwBook,
				   prvwChapNumber;
	private JButton tagsApplyBtn;
	private JSplitPane splitPane;

	
	
	private Controller controller;
	
	public SearchWindow(Controller _controller){
		controller = _controller;
		
	}
	
	public void init(){
		
		getWindow().add(getContainer());
		getWindow().setVisible(true);
	}
	
	private JFrame getWindow(){
		if(mainWindow==null){
			mainWindow=new JFrame("Religious Text Search Pro");
			mainWindow.setMinimumSize(new Dimension(1100,650));
			mainWindow.setExtendedState(mainWindow.getExtendedState() | mainWindow.MAXIMIZED_BOTH);
			//mainWindow.setResizable(false);
			mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			mainWindow.setLocation(new Point(20, 20));
			mainWindow.setJMenuBar(new MenuBar().getMenuBar(mainWindow, controller));
			
			java.net.URL imageURL = this.getClass().getResource("/img/icon.png");
			ImageIcon image = new ImageIcon(imageURL);
			mainWindow.setIconImage(image.getImage());
		}
		return mainWindow;
	}
	
	
	private JPanel getContainer(){
		if(container==null){
			container= new JPanel();
			container.setLayout(new BorderLayout(0,0));
			container.add(getSearchOptions(),BorderLayout.WEST);
			container.add(getResults(),BorderLayout.CENTER);
		}
		return container;
	}
	
	
	private JPanel getSearchOptions(){
		if(optionsPanel==null){
			optionsPanel=new JPanel();
			optionsPanel.setMinimumSize(new Dimension(200,600));
			optionsPanel.setPreferredSize(new Dimension(200,600));
			
			java.net.URL imageURL = this.getClass().getResource("/img/rtspLogo.png");
			JLabel picLabel = new JLabel(new ImageIcon(imageURL));
			
			searchTextField=new JTextField(20);
			searchTextField.setText("Search...");
			searchTextField.addFocusListener(this);
			searchTextField.addKeyListener(this);
			
			optionsPanel.setLayout(new FlowLayout());
			
			optionsPanel.add(picLabel);
			optionsPanel.add(searchTextField);

		}
		return optionsPanel;
	}
	
	private JPanel getResults(){
		if(resultsPanel==null){
			resultsPanel =new JPanel(new BorderLayout());
			
			String[] data1 = {"Testaments"};
			String[] data2 = {"Books"};
			String[] chapterData = {"Chapter:Verse Number"};
			
			religiousTexts = new JList(data1);
			religiousTexts.setLayoutOrientation(JList.VERTICAL);
			religiousTexts.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			religiousTexts.addListSelectionListener(this);
			JScrollPane rtPane = new JScrollPane(religiousTexts);
			

			
			books = new JList(data2);
			books.setLayoutOrientation(JList.VERTICAL);
			books.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			books.addListSelectionListener(this);
			JScrollPane vPane = new JScrollPane(books);
			

			
			chapters = new JList(chapterData);
			chapters.setLayoutOrientation(JList.VERTICAL);
			
			chapters.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			chapters.addListSelectionListener(this);
			JScrollPane cPane = new JScrollPane(chapters);


			
			JPanel tagOptions = new JPanel();
			tagsTextField = new JTextField(45);
			tagsLabel = new JLabel("Tags:");
			tagsApplyBtn = new JButton();
			tagsApplyBtn.setText("Apply");
			tagsApplyBtn.setActionCommand("tagsApply");
			tagsApplyBtn.addActionListener(this);
			tagOptions.add(tagsLabel);
			tagOptions.add(tagsTextField);
			tagOptions.add(tagsApplyBtn);
			
			
			JPanel previewDescription = new JPanel(new BorderLayout());
			prvwTstmt = new JLabel("Testament - ");
			prvwBook = new JLabel("Book - ");
			prvwChapNumber = new JLabel("Chapter:Verse - ");
			previewDescription.add(prvwTstmt, BorderLayout.PAGE_START);
			previewDescription.add(prvwBook, BorderLayout.CENTER);
			previewDescription.add(prvwChapNumber, BorderLayout.PAGE_END);
			
			preview = new JTextPane();
			preview.setContentType("text/html");

			preview.setEditable(false);
			JScrollPane areaScrollPane = new JScrollPane(preview);
			areaScrollPane.setVerticalScrollBarPolicy(
					JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

			previewPanel = new JPanel(new BorderLayout());
			previewPanel.add(previewDescription, BorderLayout.PAGE_START);
			previewPanel.add(areaScrollPane, BorderLayout.CENTER);
			previewPanel.add(tagOptions, BorderLayout.PAGE_END);
			previewPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
			
			JPanel wrapper = new JPanel(new BorderLayout());
			
			String layoutDef = 
			    "(ROW (LEAF name=left weight=0.2) (LEAF name=centre weight=0.6) (LEAF name=right weight=0.2))";
			MultiSplitLayout.Node modelRoot = 
			    MultiSplitLayout.parseModel(layoutDef);
		                
		        MultiSplitPane multiSplitPane = new MultiSplitPane();
		        multiSplitPane.getMultiSplitLayout().setModel(modelRoot);
		       	multiSplitPane.add(rtPane, "left");
		       	multiSplitPane.add(vPane, "centre");
		       	multiSplitPane.add(cPane, "right");

			wrapper.add(multiSplitPane, BorderLayout.CENTER);
			
			splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
					wrapper, previewPanel);
			splitPane.setResizeWeight(0.5);



			resultsPanel.add(splitPane,BorderLayout.CENTER);
			resultsPanel.setBorder(BorderFactory.createTitledBorder("Search Results"));
		}
		return resultsPanel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("tagsApply")){
			controller.updateTags(tagsTextField.getText());
		}
	}

	@Override
	public void focusGained(FocusEvent e) {
		if(searchTextField.getText().equals("Search...")){
			searchTextField.setText("");
		}
	}

	@Override
	public void focusLost(FocusEvent e) {
		if(searchTextField.getText().trim().equals("")){
			searchTextField.setText("Search...");
		}
	}
	
	@Override
	public void update(Observable o, Object searchResults) {
		
		Model mod = (Model) o;
		religiousTexts.setListData(mod.getTstmtsResults());
		books.setListData(mod.getBookResults());
		chapters.setListData(mod.getChapterResults());
		
	}

	public void valueChanged(ListSelectionEvent lse) {
		String[] info = controller.getPreview(chapters.getSelectedIndex());
			preview.setText(info[0]);
			prvwTstmt.setText("Testament - " + info[1]);
			prvwBook.setText("Book - " + info[2]);
			prvwChapNumber.setText("Chapter:Verse - " + info[3]);
			tagsTextField.setText(info[4]);

			controller.applyBookFilter(books.getSelectedIndex());
			controller.applyTstmtFilter(religiousTexts.getSelectedIndex());
	}

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {
		if(searchTextField.getText().equals("")){
			String[] data1 = {"Testaments"};
			String[] data2 = {"Books"};
			String[] chapterData = {"Chapter:Verse Number"};
			religiousTexts.setListData(data1);
			books.setListData(data2);
			chapters.setListData(chapterData);
			preview.setText("");
		}
		controller.searchPressed(searchTextField.getText());
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

}
